package com.zzyl.common.utils;

import java.time.LocalDateTime;
import java.time.ZoneId;

public class TimeUtil {

    public static LocalDateTime[] createLocalDateTimeFromHours(int startTime, int endTime) {
        // 获取当前日期
        LocalDateTime now = LocalDateTime.now(ZoneId.systemDefault());

        // 构造开始时间和结束时间的 LocalDateTime 对象
        LocalDateTime startLocalDateTime = now.withHour((startTime))
                                              .withMinute(0)
                                              .withSecond(0)
                                              .withNano(0);
        LocalDateTime endLocalDateTime = now.withHour((endTime))
                                            .withMinute(59)
                                            .withSecond(59)
                                            .withNano(999_999_999);

        // 返回包含开始时间和结束时间的 LocalDateTime 数组
        return new LocalDateTime[]{startLocalDateTime, endLocalDateTime};
    }

    public static void main(String[] args) {
        // 示例调用
        int startHour = 1; // 用户选择的开始小时，从前端获取
        int endHour = 23;  // 用户选择的结束小时，从前端获取

        LocalDateTime[] localDateTimes = createLocalDateTimeFromHours(startHour, endHour);

        System.out.println("Start LocalDateTime: " + localDateTimes[0]);
        System.out.println("End LocalDateTime: " + localDateTimes[1]);

        // 如果需要转换为毫秒时间戳
        long startTimeMillis = localDateTimes[0].atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        long endTimeMillis = localDateTimes[1].atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();

        System.out.println("Start Time Millis: " + startTimeMillis);
        System.out.println("End Time Millis: " + endTimeMillis);
    }
}